package com.bsdk.install.device.electricmeter.v1.cmd;

import com.alibaba.fastjson2.JSONObject;
import com.bsdk.common.utils.StringUtils;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;

/**
 * @projectName: lock
 * @package: com.hlslock.lock.cmd
 * @className: Cmd
 * @author: HansYoung
 * @description: TODO
 * @date: 2023/12/6 14:14
 * @version: 1.0
 */

@NoArgsConstructor
@Getter
public class COMMAND extends Cmd {
    byte[] data = new byte[100];
    /*
    获取MQTT的登录信息
    */
    public String Gw_get_Mqtt() {

        int j;
        j = 0;
        data[j++] = GW_CMD_MQTT_REQ;//查询
        return pack_crc(data, j);
    }

    /*
    MQTT的登录信息
    m_Clent_id = "7f40ae"; m_User = "7f40ae"; m_pw = "902217";
    string m_Ip = "139.9.65.95";
    int port = 19240;
     */
    public String Gw_set_Mqtt(String m_Devid, String m_User, String m_Pw, String m_Ip, int m_Port) {
        int j, i;
        int len;
        byte[] array = new byte[100];
        j = 0;


        data[j++] = GW_CMD_MQTT_SET;
        //m_Clent_id
        array = m_Devid.getBytes();
        len = array.length;
        data[j++] = (byte) len;
        for (i = 0; i < len; i++) {
            data[j++] = array[i];
        }
        //m_User
        array = m_User.getBytes();
        len = array.length;
        data[j++] = (byte) len;
        for (i = 0; i < len; i++) {
            data[j++] = array[i];
        }
        //m_pw
        array = m_Pw.getBytes();
        len = array.length;
        data[j++] = (byte) len;
        data[j++] = (byte) len;
        for (i = 0; i < len; i++) {
            data[j++] = array[i];
        }
        //m_Ip
        array = m_Ip.getBytes();
        len = array.length;
        data[j++] = (byte) len;
        data[j++] = (byte) len;
        for (i = 0; i < len; i++) {
            data[j++] = array[i];
        }
        //port
        data[j++] = (byte) (m_Port % 0x100);
        data[j++] = (byte) ((m_Port / 0x100) & 0xFF);
        return pack_crc(data, j);


    }

    public String Gw_get_Server() {
        int j;
        j = 0;
        data[j++] = GW_CMD_AUTH_IP_PORT_REQ;//查询
        return pack_crc(data, j);
    }

    /*
    string url = "http://auth.etom-link.com:19200/auth/data/mac/";
    获取连接IP、端口等的预埋设置
     */
    public String Gw_set_Server(String auth_url) {
        int j, i;
        j = 0;

        int len;
        //string url = "http://auth.etom-link.com:19200/auth/data/mac/";
        byte[] array = auth_url.getBytes();
        len = array.length;

        data[j++] = GW_CMD_AUTH_IP_PORT_SET;
        for (i = 0; i < len; i++) data[j++] = array[i];
        data[j++] = 0;//字符串结尾是0
        return pack_crc(data, j);

    }

    public String Gw_Req_Info() {
        int j;
        j = 0;
        data[j++] = GW_CMD_INFO_REQ;//查询
        return pack_crc(data, j);
    }

    /*
     出厂前的设置
     devid = "1001580f80"; apikey = "11071915-3316-4d23-9dc9-13e51e10fc4b"; chipid = "202304241931";//1
     */
    public String Gw_set_Factory(String m_devid, String m_apikey, String m_chipID) {
        int j, i;
        j = 0;
        byte[] array = new byte[100];
        int len;


        data[j++] = GW_CMD_DEVID_SET;

        //devid
        array = m_devid.getBytes();
        len = array.length;
        data[j++] = (byte) len;
        data[j++] = (byte) len;
        for (i = 0; i < len; i++) {
            data[j++] = array[i];
        }
        //apikey
        array = m_apikey.getBytes();
        len = array.length;
        data[j++] = (byte) len;
        data[j++] = (byte) len;
        for (i = 0; i < len; i++) {
            data[j++] = array[i];
        }
        //chipid
        array = m_chipID.getBytes();
        len = array.length;
        data[j++] = (byte) len;
        data[j++] = (byte) len;
        for (i = 0; i < len; i++) {
            data[j++] = array[i];
        }
        data[j++] = GW_CMD_DEVID_REQ;//查询
        return pack_crc(data, j);

    }

    public String Gw_Req_Factory() {
        int j, i;
        j = 0;
        data[j++] = GW_CMD_DEVID_REQ;//查询
        return pack_crc(data, j);
    }

    /**
     * 下载的路径，格式如下：
     * //str_url = "http://120.79.155.152:8008//upload/upgrade/flashimage-ota-xz-v0.1.bin";
     * // str_url = "http://wx.hlslock.com:80/uploadfw/20230826/flashimage-ota-xz-v0.4.bin";
     * 注意http,非https
     *
     * @param
     * @return
     */
    public String Gw_OTA_Upgrade(String str_url) {
        int j, i;
        int len;
        j = 0;
        byte[] array = str_url.getBytes();
        len = array.length;
        data[j++] = GW_CMD_DOWN_START;
        for (i = 0; i < len; i++) data[j++] = array[i];
        data[j++] = 0;//以0结束
        return pack_crc(data, j);
    }

    /**
     * 复位网关
     *
     * @return
     */
    public String Gw_Reset_System() {
        int j;
        j = 0;
        data[j++] = GW_CMD_RESET_GW;
        return pack_crc(data, j);

    }

    /**
     * 取服务器的时间
     */
    public String Gw_Set_Time() {
        int j;
        j = 0;

        // 下发当前的时间
        LocalDateTime localDateTime = LocalDateTime.now().plusSeconds(5);
        data[j++] = GW_CMD_TIME_SET;//设置时间
        data[j++] = (byte) (localDateTime.getYear() / 0x100);
        data[j++] = (byte) (localDateTime.getYear() % 0x100);
        data[j++] = (byte) localDateTime.getMonthValue();// 12月
        data[j++] = (byte) localDateTime.getDayOfMonth();// 10日
        data[j++] = (byte) localDateTime.getHour();// 17点
        data[j++] = (byte) localDateTime.getMinute();// 25分
        data[j++] = (byte) localDateTime.getSecond();// 00秒
        return pack_crc(data, j);
    }

    /*
    获取时间
     */
    public String Gw_Get_Time() {
        int j;
        j = 0;
        data[j++] = GW_CMD_TIME_REQ;//查询
        return pack_crc(data, j);
    }


}
